package com.hwua.view;

import com.hwua.entity.Student;
import com.hwua.entity.Teacher;
import com.hwua.service.StudentService;
import com.hwua.service.TeacherService;
import com.hwua.service.impl.StudentServiceImpl;
import com.hwua.service.impl.TeacherServiceImpl;
import com.hwua.tqm.FatherTable;

import java.sql.SQLException;
import java.util.List;

/**
 * @author CQY
 */
public class StudentView extends FatherTable{

    @Override
    public String[] columnNamesInit() {
        String[] s={"学号","姓名","年龄","性别"};
        return s;
    }

    @Override
    public Object[][] tableDataInit() {
        StudentService service=new StudentServiceImpl();
        List<Student> list=service.findAll();
        int row=list.size();
        Object[][] date=new Object[row][4];
        for (int i = 0; i <date.length ; i++) {
            Student student=list.get(i);
            date[i][0]=student.getStuId();
            date[i][1]=student.getStuName();
            date[i][2]=student.getStuAge();
            date[i][3]=student.getStuSex();
        }
        return date;
    }

    @Override
    public void deleteMoreAction(Object[] ids) {

        StudentService service=new StudentServiceImpl();
        for (int i = 0; i <ids.length ; i++) {
            Student student=new Student();
            try {
                service.delete((String)ids[i]);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void addSaveAction(Object[] obj) {
        StudentService service=new StudentServiceImpl();
        String id=(String) obj[0];
        String name=(String)obj[1];
        String age=(String)obj[2];
        String sex=(String)obj[3];
        Student student=new Student(id,name,age,sex);
        int row=service.save(student);

    }

    @Override
    public void updateSaveAction(Object[] obj) {
        StudentService service=new StudentServiceImpl();
        String id=(String) obj[0];
        String name=(String)obj[1];
        String age=(String)obj[2];
        String sex=(String)obj[3];
        Student student=new Student(id,name,age,sex);
        int row=service.update(student);

    }

    @Override
    public Object[][] searchAction(String keyword) {
        StudentService service=new StudentServiceImpl();
        List<Student> list=service.findSearch(keyword);
        int row=list.size();
        Object[][] date=new Object[row][4];
        for (int i = 0; i <date.length ; i++) {
            Student student=list.get(i);
            date[i][0]=student.getStuId();
            date[i][1]=student.getStuName();
            date[i][2]=student.getStuAge();
            date[1][3]=student.getStuSex();
        }
        return date;
    }

    @Override
    public void deleteAction(Object id) {

        StudentService service=new StudentServiceImpl();
            Student student=new Student();
        try {
            service.delete((String)id);
        } catch (SQLException e) {
            e.printStackTrace();

        }

    }
}
